package OusloginRegister;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;

@WebServlet(name = "bookRoom",urlPatterns = "/book")
public class bookRoom extends HttpServlet {
    java.sql.Connection conn = null;
    String strConn;
    java.sql.Statement sqlStmt = null;
    java.sql.Statement sqlStmt1 = null;
    ResultSet sqlRst = null;
    ResultSet sqlRst1 = null;
    static long ono;
    static
    {
        ono =10000000;
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        try{
            conn =java.sql.DriverManager.getConnection("jdbc:h2:~/jpa-demo-db;MODE=MYSQL;DB_CLOSE_DELAY=-1","sa","sa");
            sqlStmt = conn.createStatement();
            sqlStmt1 = conn.createStatement();
            if(request.getParameter("submit")!=null&&request.getSession().getAttribute("PHONE")!=null)
            {
                String name = request.getSession().getAttribute("NAME").toString();
                String room = request.getParameter("room");
                if(room.equals("big")) room ="大床房";
                else if(room.equals("single")) room="单人间";
                else if(room.equals("standard")) room="标准间";
                else room= "客房";
                String checkIn = request.getParameter("checkIn");
                String checkOut = request.getParameter("checkOut");
                System.out.print(room+" "+checkIn+" "+checkOut);
                String sqlQuery ="select rno,price from room where style="+"'"+room+"'"+"and status ="+"'"+"空"+"'";
                sqlRst = sqlStmt.executeQuery(sqlQuery);
                if(sqlRst.next())
                {
                    System.out.println("有房\n");
                    String sql3= "select ono from order1";
                    sqlRst1= sqlStmt1.executeQuery(sql3);
                    while(sqlRst1.next()) ono=sqlRst1.getInt("ono");//检擦订单编号最大一位
                    System.out.println("找到编号");
                    ono++;
                    String rno = sqlRst.getString("RNO");
                    int price =sqlRst.getInt("PRICE");
                    String sql = "update room set status="+"'"+"住"+"'"+"where rno = "+"'"+rno+"'";
                    sqlStmt.executeUpdate(sql);
                    String phone = request.getSession().getAttribute("PHONE").toString();
                    int count=1;
                    if(checkIn.substring(0,2).equals(checkOut.substring(0,2))) {
                        count = Integer.parseInt(checkOut.substring(3,5)) - Integer.parseInt(checkIn.substring(3,5));
                        if(count <=0) count=1;
                    }
                    price =price*count;
                    String in = checkIn.substring(6,checkIn.length())+"-"+checkIn.substring(0,2)+"-"+checkIn.substring(3,5);
                    String out = checkOut.substring(6,checkOut.length())+"-"+checkOut.substring(0,2)+"-"+checkOut.substring(3,5);
                    String sql1 = "insert into order1(ono,name,phone,rno,check_time,de_time,price,pledge,count)"+
                            "values("+ono+","+"'"+name+"',"+"'"+phone+"'"+","+"'"+rno+"',"+"'"+in+"',"+"'"+out+"',"+"'"+price+"',"
                            +500+","+count+")";
                    sqlStmt.executeUpdate(sql1);
//                   response.getWriter().write("预订成功！\n");
//                   response.getWriter().write("房间编号为"+rno+"\n");
                    response.getWriter().print("<script> alert(\"预定成功！房间编号为:\"); </script>"+rno.toString());
                    response.setHeader("refresh", "3;url=http://localhost:8084/hotel.html");
//                    response.sendRedirect("http://localhost:8084/hotel.html");
                }
                else
                {
                    System.out.println("此类房型暂无房间\n");
                    response.getWriter().print("<script> alert(\"此类房型暂无房间\"); </script>");
                    response.setHeader("refresh", "3;url=http://localhost:8084/index.jsp");
//                    System.out.println("此类房型暂无房间\n");
//                    response.sendRedirect("http://localhost:8084/index.jsp");
                }
            }
        }
        catch (Exception ex)
        {
            System.out.println(ex.toString());
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
